package com.guigu.xiaowenbo.controller;

import com.guigu.aop.OpLog;
import com.guigu.liuzhireng.pojo.Account;
import com.guigu.util.PageVo;
import com.guigu.util.service.IDService;
import com.guigu.xiaowenbo.pojo.Scell;
import com.guigu.xiaowenbo.pojo.Sgather;
import com.guigu.xiaowenbo.pojo.Spay;
import com.guigu.xiaowenbo.pojo.Spaydetails;
import com.guigu.xiaowenbo.service.ScellService;
import com.guigu.xiaowenbo.service.SpayService;
import com.guigu.xiaowenbo.service.SpaydetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class SpayController {
    @Autowired
    SpayService spayService;
    @Autowired
    SpaydetailsService spaydetailsService;
    @Autowired
    IDService idService;
    @Autowired
    ScellService scellService;
    @RequestMapping("rkre")
    public ModelAndView rkr(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        String acc = ((Account) session.getAttribute("user")).getAccount();
        modelAndView.addObject("name",acc);
        modelAndView.setViewName("xiao_wen_bo/demore.jsp");
        return modelAndView;
    }
    @RequestMapping("insertre")
    @ResponseBody
    @OpLog(opModule = "出库申请模块",operType = "添加出库申请",operDesc = "此方法用于添加出库申请")
    public void insertsg(@RequestBody Spay spay, HttpSession session){
        String strid = idService.Stock_Out_ID();
        spay.setPayid(strid);
        int id = spayService.insertre(spay);
        session.setAttribute("spayID",id);
    }
    @RequestMapping("insertres")
    @ResponseBody
    @OpLog(opModule = "出库申请模块",operType = "添加出库申请明细",operDesc = "此方法用于添加出库申请明细")
    public int insertsgd(@RequestBody Spaydetails[] spaydetails, HttpSession session){
        Integer id = (Integer) session.getAttribute("spayID");
        for (Spaydetails spaydetail : spaydetails) {
            System.out.println(spaydetail);
            spaydetail.setParentid(id);
            spaydetailsService.insertres(spaydetail);
        }
        return 1;
    }
    @RequestMapping("selectrw")
    @ResponseBody
    public PageVo selectrw(@RequestBody PageVo<Spay> spay){
        return spayService.selectrw(spay.getNum(),spay.getSize());
    }
    @RequestMapping("updaterw")
    @OpLog(opModule = "出库申请模块",operType = "出库申请审核",operDesc = "此方法用于添加出库申请审核")
    public ModelAndView updatesg(Spay spay, String[] productid, String[] amount){
        Integer ab=1;
        ModelAndView modelAndView = new ModelAndView();
        if (spay.getChecktag().equals("S001-2")){
            spayService.updaterw(spay);
        }else {
            for (int i=0;i<productid.length;i++){
                Scell scell= scellService.selectByPrimaryKeyfhcxid(productid[i]);
                Integer am = scell.getAmount();
                if (am<Integer.parseInt(amount[i])){
                    ab=2;
                }
            }
            System.out.println(spay.getId());
            System.out.println(ab);
            if (ab==1){
                spayService.updaterw(spay);
            }else {
                modelAndView.addObject("errors","申请数量大于现存储量");
                modelAndView.setViewName("selectrws?id="+spay.getId());
                return modelAndView;
            }
        }
        modelAndView.setViewName("xiao_wen_bo/selectrw.jsp");
        return modelAndView;
    }
    @RequestMapping("selectrwe")
    @ResponseBody
    public PageVo selectrwe(@RequestBody PageVo<Spay> spay){
        return spayService.selectrwe(spay.getNum(),spay.getSize());
    }
    @RequestMapping("selectrwdd")
    @ResponseBody
    public PageVo selectrwdd(@RequestBody PageVo<Spay> spay){
        return spayService.selectrwdd(spay.getNum(),spay.getSize());
    }
    @RequestMapping("selectrwrs")
    @ResponseBody
    public PageVo selectrwrs(@RequestBody PageVo<Spay> spayPageVo){
        return spayService.selectrwrs(spayPageVo.getNum(),spayPageVo.getSize());
    }
    @RequestMapping("updaterwre")
    @OpLog(opModule = "出库管理模块",operType = "出库管理登记",operDesc = "此方法用于添加出库管理登记")
    public ModelAndView updaterwre(String register, String payid, String[] productid, Integer[] parentid, Integer[] paidamount, String[] amount, Integer[] paidamounts){
        ModelAndView modelAndView = new ModelAndView();
        for (int i=0;i<parentid.length;i++){
            Integer a =Integer.parseInt(amount[i])-paidamount[i];
            if (a<paidamounts[i]){
                System.out.println(parentid[i]);
                modelAndView.addObject("error","入库数量大于当前可存放数量或应入库数量");
                modelAndView.setViewName("selectrwdrs?parentid="+parentid[i]);
                return modelAndView;
            }else {
                spaydetailsService.updaterwdrses(parentid[i],productid[i],paidamounts[i]);
            }
        }
        spayService.updaterwre(payid,register);
        modelAndView.setViewName("xiao_wen_bo/selectrwrs.jsp");
        return modelAndView;
    }
    @RequestMapping("selectrwrssh")
    @ResponseBody
    public PageVo selectrwrssh(@RequestBody PageVo<Sgather> sgatherPageVo){
        return spayService.selectrwrssh(sgatherPageVo.getNum(),sgatherPageVo.getSize());
    }
    @RequestMapping("updaterwresh")
    @OpLog(opModule = "出库管理模块",operType = "出库管理登记审核",operDesc = "此方法用于出库管理登记审核")
    public String updaterwresh(Spay spay, Integer[] parentid, String[] productid, Integer[] paidamount, Integer[] paidamounts, Integer[] amount){
        Integer num=1;
        Integer amsum = 0;
        for (int i=0;i<parentid.length;i++){
            amsum+=paidamounts[i];
            spaydetailsService.updaterwdrs(parentid[i],productid[i],paidamounts[i]);
            spaydetailsService.updaterwdrses(parentid[i],productid[i],0);
            scellService.updatescrw(productid[i],paidamounts[i]);
            Integer ga = amount[i]-paidamount[i];
            if (paidamounts[i]<ga){
                num+=1;
            }
        }
        System.out.println(amsum);
        spay.setPaidamountsum(amsum);
        System.out.println(num);
        if (num==1){
            spayService.updaterwresh(spay);
        }else {
            spayService.updaterwreshs(spay);
        }
        return "xiao_wen_bo/selectrwrssh.jsp";
    }
    @RequestMapping("selectrwres")
    @ResponseBody
    public PageVo selectrwres(@RequestBody PageVo<Sgather> sgatherPageVo){
        return spayService.selectrwres(sgatherPageVo.getNum(),sgatherPageVo.getSize());
    }
}
